﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.LopService;

namespace eTMS.Forms.Students
{
    public partial class FrmSinhVienImport : eTMS.Controls.BaseForm
    {
        public FrmSinhVienImport()
        {
            InitializeComponent();
        }



        private void btnBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = eTMS.Commons.Constants.FILTER_EXCEL_FILE;
            open.Multiselect = false;
            if (open.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = open.FileName;
                DataTable tblSheet = Commons.ExcelReader.ReadExcellSheet(txtFileName.Text);
                List<string> ls = new List<string>();
                foreach (DataRow r in tblSheet.Rows)
                {
                    string st = r["TABLE_NAME"].ToString().Trim();
                    st = st.TrimStart('\'');
                    int index = st.IndexOf("$");
                    if (index > 0)
                    {
                        st = st.Substring(0, index);
                    }
                    st = st.TrimEnd('\'');
                    if (!ls.Contains(st))
                        ls.Add(st);
                }
                txtTenSheet.DataSource = ls;
            }
        }

        private void txtTenSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtTenSheet.SelectedIndex >= 0)
            {
                DataTable tblColumns = Commons.ExcelReader.ReadExcellColumn(txtFileName.Text, txtTenSheet.Text);
                txtMaSo.DisplayMember = "COLUMN_NAME";
                txtMaSo.DataSource = tblColumns.Copy();
                txtDiaChi.DisplayMember = "COLUMN_NAME";
                txtDiaChi.DataSource = tblColumns.Copy();
                txtDienThoai.DisplayMember = "COLUMN_NAME";
                txtDienThoai.DataSource = tblColumns.Copy();
                txtEmail.DisplayMember = "COLUMN_NAME";
                txtEmail.DataSource = tblColumns.Copy();
                txtGioiTinh.DisplayMember = "COLUMN_NAME";
                txtGioiTinh.DataSource = tblColumns.Copy();
                txtHoTen.DisplayMember = "COLUMN_NAME";
                txtHoTen.DataSource = tblColumns.Copy();
                txtNgaySinh.DisplayMember = "COLUMN_NAME";
                txtNgaySinh.DataSource = tblColumns.Copy();
                txtLop.DisplayMember = "COLUMN_NAME";
                txtLop.DataSource = tblColumns.Copy();
                txtSoCMND.DisplayMember = "COLUMN_NAME";
                txtSoCMND.DataSource = tblColumns.Copy();
            }
        }
        SinhVienController controller;
        private void btnRead_Click(object sender, EventArgs e)
        {
            int NganhHoc_ID = -1;
            if (treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID > 0)
            {
                NganhHoc_ID = treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID;
            }
            else if (treeKhoaNganhKhoaHoc1.SelectedLop_ID > 0)
            {
                NganhHoc_ID = (treeKhoaNganhKhoaHoc1.GetSelectedLop().NganhHoc_ID);
            }

            DataSet ds = Commons.ExcelReader.ReadExcellFile(txtFileName.Text, txtTenSheet.Text);
            controller.ResetTable();
            controller.Table.Columns.Add("Lop_ID", typeof(int));
            controller.Table.Columns.Add("TenLop", typeof(string));
            controller.Table.Columns.Add("TenGioiTinh", typeof(string));
            controller.Table.Columns.Add("NganhHoc_ID", typeof(int));
            controller.Table.Columns.Add("KhoaHoc_ID", typeof(int));
            List<LopEntity> lsLop = treeKhoaNganhKhoaHoc1.GetSelectedListLop();
            if (ds.Tables.Count > 0)
            {

                DataTable tblTemp = ds.Tables[0];

                if (tblTemp.Columns.Contains(txtLop.Text))
                {
                    lsLop = treeKhoaNganhKhoaHoc1.GetSelectedListLop();
                }
                foreach (DataRow r in tblTemp.Rows)
                {
                    DataRow row = controller.Table.NewRow();
                    row["SinhVien_ID"] = 0;
                    row["NganhHoc_ID"] = NganhHoc_ID;
                    row["KhoaHoc_ID"] = treeKhoaNganhKhoaHoc1.KhoaHoc_ID;
                    if (tblTemp.Columns.Contains(txtHoTen.Text))
                    {
                        if (r[txtHoTen.Text].ToString().Trim().Length > 0)
                        {
                            if (tblTemp.Columns.Contains(txtMaSo.Text))
                            {
                                row["MaSinhVien"] = r[txtMaSo.Text];
                            }
                            row["TenSinhVien"] = r[txtHoTen.Text];
                            if (tblTemp.Columns.Contains(txtNgaySinh.Text))
                            {
                                row["NgaySinh"] = r[txtNgaySinh.Text];
                            }
                            if (tblTemp.Columns.Contains(txtDiaChi.Text))
                            {
                                row["DiaChi"] = r[txtDiaChi.Text];
                            }
                            if (tblTemp.Columns.Contains(txtDienThoai.Text))
                            {
                                row["DienThoai"] = r[txtDienThoai.Text];
                            }
                            if (tblTemp.Columns.Contains(txtEmail.Text))
                            {
                                row["Email"] = r[txtEmail.Text];
                            }
                            if (tblTemp.Columns.Contains(txtGioiTinh.Text))
                            {
                                bool isNu = r[txtGioiTinh.Text].ToString().Trim().ToLower() == txtNu.Text.Trim().ToLower();
                                row["TenGioiTinh"] = isNu ? "Nữ" : "Nam";
                                row["GioiTinh"] = isNu ? 0 : 1;
                            }
                            else
                            {
                                row["TenGioiTinh"] = "";
                                row["GioiTinh"] = 2;
                            }
                            if (tblTemp.Columns.Contains(txtSoCMND.Text))
                            {
                                row["SoCMND"] = r[txtSoCMND.Text];
                            }
                            LopEntity oLop = treeKhoaNganhKhoaHoc1.GetSelectedLop();
                            if (oLop != null)
                            {
                                row["Lop_ID"] = oLop.Lop_ID;
                                row["TenLop"] = oLop.TenLop;
                                row["NganhHoc_ID"] = oLop.NganhHoc_ID;
                            }
                            if (tblTemp.Columns.Contains(txtLop.Text))
                            {
                                oLop = LopController.FindLopByTenLop(r[txtLop.Text].ToString(), lsLop);
                                if (oLop != null)
                                {
                                    row["TenLop"] = r[txtLop.Text];
                                    row["Lop_ID"] = oLop.Lop_ID;
                                    row["NganhHoc_ID"] = oLop.NganhHoc_ID;
                                }
                            }
                            if (controller.ConvertInt(row["NganhHoc_ID"]) <= 0)
                            {
                                if (!Confirm("Sinh viên " + r[txtHoTen.Text].ToString() + " không tìm thấy lớp hoặc ngành học tướng ứng! Bạn có muốn tiếp tục"))
                                    return;
                            }
                            else
                            {
                                controller.Table.Rows.Add(row);
                            }
                        }
                    }
                }
            }
            controller.AddIndexToTable();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = controller.Table;
        }

        private void FrmSinhVienImport_Load(object sender, EventArgs e)
        {

            controller = new SinhVienController();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (controller.SaveSinhVienTable())
            {
                ShowInfo("Bạn đã lưu danh sách sinh viên thành công!");
                controller.Table = new DataTable();
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = null;
            }
            else
            {
                ShowWarning("Có lỗi xảy ra khi lưu dữ liệu!");
            }
        }



    }
}
